home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-04 | 14.3 KB | 491 lines | [TEXT/MMCC] |
- //• --------------------------------------------------------------------- •//
- //• --------------------------------------------------------------------- •//
- //• Updated to run in THINK C™ v.5.0.4, changed to an applicationand
- //• redistributed as a public domain C source demo, on 16 February 1994,
- //• by Kenneth A. Long.
- //• --------------------------------------------------------------------- •//
- //• --------------------------------------------------------------------- •//
- //• Ascii Chart - a Desk Accessory for the Macintosh written by:.
- //• Wade S. Blomgren (c) 1986 version 1.0.
- //• UC San Diego.
-
- //• Both source and executable files may be freely distributed for.
- //• NON-COMMERCIAL USE ONLY provided this copyright notice is left.
- //• intact..
-
- //• This program is intended as an example for persons beginning to.
- //• learn about programming the Macintosh. It is NOT meant to be an.
- //• example of excellent programming or a particularly fabulous.
- //• idea for a program. It IS supposed to be chock full of comments.
- //• and to have some examples of common uses for the toolbox..
-
- //• This program was written using LightspeedC (c) from Think.
- //• Technologies, INC. and therefore includes material that is.
- //• copyright Think Technologies INC..
-
- //• Some techniques were gleaned from a PD desk accessory called.
- //• ZoomIdle by Paul DuBois, University of Wisconsin.
-
- //• Wade S. Blomgren UCSD-ACS mailcode B-028 La Jolla, CA 92093.
- //• --------------------------------------------------------------------- •//
-
- //• Since this is a DA and not a standalone application we do not have to
- //• initialize the various managers. (Yeah! Right! (kal) :)
-
- //• Global Variables for the DA. (What DA? (kal) :)
-
- //• The chart data is stored in two dimensional arrays so we can use the.
- //• drawtext routine (faster than repeated calls to drawchar) (IM I-172).
- //• --------------------------------------------------------------------- •//
-
- char octdata[23][47] = {
- "000 NUL 026 SYC 054 , 102 B 130 X 156 n ",
- "001 SOH 027 ETB 055 - 103 C 131 Y 157 o ",
- "002 STX 030 CAN 056 . 104 D 132 Z 160 p ",
- "003 ETX 031 EM 057 / 105 E 133 [ 161 q ",
- "004 EOT 032 SUB 060 0 106 F 134 \\ 162 r ", //• escape the \ character.
- "005 ENQ 033 ESC 061 1 107 G 135 ] 163 s ",
- "006 ACK 034 FS 062 2 110 H 136 ^ 164 t ",
- "007 BEL 035 GS 063 3 111 I 137 _ 165 u ",
- "010 BS 036 RS 064 4 112 J 140 ` 166 v ",
- "011 HT 037 US 065 5 113 K 141 a 167 w ",
- "012 LF 040 SPA 066 6 114 L 142 b 170 x ",
- "013 VT 041 ! 067 7 115 M 143 c 171 y ",
- "014 FF 042 \" 070 8 116 N 144 d 172 z ", //• escape the " character.
- "015 CR 043 # 071 9 117 O 145 e 173 { ",
- "016 SO 044 $ 072 : 120 P 146 f 174 | ",
- "017 SI 045 % 073 ; 121 Q 147 g 175 } ",
- "020 DLE 046 & 074 < 122 R 150 h 176 ~ ",
- "021 DC1 047 ' 075 = 123 S 151 i 177 DEL",
- "022 DC2 050 ( 076 > 124 T 152 j ",
- "023 DC3 051 ) 077 ? 125 U 153 k ",
- "024 DC4 052 * 100 @ 126 V 154 l ",
- "025 NAK 053 + 101 A 127 W 155 m "
- };
-
- char decdata[23][47] = {
- "000 NUL 022 SYC 044 , 066 B 088 X 110 n ",
- "001 SOH 023 ETB 045 - 067 C 089 Y 111 o ",
- "002 STX 024 CAN 046 . 068 D 090 Z 112 p ",
- "003 ETX 025 EM 047 / 069 E 091 [ 113 q ",
- "004 EOT 026 SUB 048 0 070 F 092 \\ 114 r ", //• escape the \ character.
- "005 ENQ 027 ESC 049 1 071 G 093 ] 115 s ",
- "006 ACK 028 FS 050 2 072 H 094 ^ 116 t ",
- "007 BEL 029 GS 051 3 073 I 095 _ 117 u ",
- "008 BS 030 RS 052 4 074 J 096 ` 118 v ",
- "009 HT 031 US 053 5 075 K 097 a 119 w ",
- "010 LF 032 SPA 054 6 076 L 098 b 120 x ",
- "011 VT 033 ! 055 7 077 M 099 c 121 y ",
- "012 FF 034 \" 056 8 078 N 100 d 122 z ", //• escape the " character.
- "013 CR 035 # 057 9 079 O 101 e 123 { ",
- "014 SO 036 $ 058 : 080 P 102 f 124 | ",
- "015 SI 037 % 059 ; 081 Q 103 g 125 } ",
- "016 DLE 038 & 060 < 082 R 104 h 126 ~ ",
- "017 DC1 039 ' 061 = 083 S 105 i 127 DEL",
- "018 DC2 040 ( 062 > 084 T 106 j ",
- "019 DC3 041 ) 063 ? 085 U 107 k ",
- "020 DC4 042 * 064 @ 086 V 108 l ",
- "021 NAK 043 + 065 A 087 W 109 m "
- };
-
- char hexdata[23][47] = {
- "000 NUL 016 SYC 02C , 042 B 058 X 06E n ",
- "001 SOH 017 ETB 02D - 043 C 059 Y 06F o ",
- "002 STX 018 CAN 02E . 044 D 05A Z 070 p ",
- "003 ETX 019 EM 02F / 045 E 05B [ 071 q ",
- "004 EOT 01A SUB 030 0 046 F 05C \\ 072 r ", //• escape the \ character.
- "005 ENQ 01B ESC 031 1 047 G 05D ] 073 s ",
- "006 ACK 01C FS 032 2 048 H 05E ^ 074 t ",
- "007 BEL 01D GS 033 3 049 I 05F _ 075 u ",
- "008 BS 01E RS 034 4 04A J 060 ` 076 v ",
- "009 HT 01F US 035 5 04B K 061 a 077 w ",
- "00A LF 020 SPA 036 6 04C L 062 b 078 x ",
- "00B VT 021 ! 037 7 04D M 063 c 079 y ",
- "00C FF 022 \" 038 8 04E N 064 d 07A z ", //• escape the " character.
- "00D CR 023 # 039 9 05F O 065 e 07B { ",
- "00E SO 024 $ 03A : 050 P 066 f 07C | ",
- "00F SI 025 % 03B ; 051 Q 067 g 07D } ",
- "010 DLE 026 & 03C < 052 R 068 h 07E ~ ",
- "011 DC1 027 ' 03D = 053 S 069 i 07F DEL",
- "012 DC2 028 ( 03E > 054 T 06A j ",
- "013 DC3 029 ) 03F ? 055 U 06B k ",
- "014 DC4 02A * 040 @ 056 V 06C l ",
- "015 NAK 02B + 041 A 057 W 06D m "
- };
-
- //• allocate control handles to the various control buttons.
- ControlHandle OctPtr, HexPtr, DecPtr, BinPtr, AbPtr,
- the_control, whichControl;
-
- int line; //• counter for the text drawing loop.
- int y; //• coordinate variable for positioning the text.
- int DAisOpen = 0; //• flag so we know if the DA is already open.
-
- MenuHandle appleMenu, fileMenu, editMenu, wasteMenu;
-
- //• Define rectangles for the DA's window and the control buttons.
- //• blanket_rect is the entire data area & is used to erase the data.
-
- //• Top. Lef. Bot. Rig.
- Rect blanket_rect = { 3, 12, 220, 280 };
- Rect bounds = { 60, 60, 335, 350 };
-
- Rect OctRect = { 225, 15, 242, 65 };
- Rect HexRect = { 225, 85, 242, 135 };
- Rect DecRect = { 225, 155, 242, 205 };
- Rect BinRect = { 225, 225, 242, 275 }; //• Not used, yet.
-
- Rect AbRect = { 250, 85, 270, 205 };
- Rect windowBounds = { 40, 40, 150, 150 };
- Rect circleStart = { 10, 10, 100, 100 };
- Rect dragRect;
-
- WindowPtr ascii_window;
-
- enum {
- appleID = 1,
- fileID,
- editID,
- wasteID
- };
-
- enum {
- openItem = 1,
- closeItem,
- quitItem = 4
- };
-
- //• various functions begin here - see 'main' for calling sequence.
-
- //• Window update routine.
- //• If it's a result of a radix change, scope is 0 (update everything).
- //• If it is a system update event, update only the area that needs it,
- //• by using BeginUpdate & EndUpdate.
- //• (see calls to 'Update_The_Display()' in 'main' and 'DAEvent'.
-
- Update_The_Display(int scope)
- {
- GrafPtr currentPort;
- Ptr dataAddr; //• a generic 'pointer' data type.
-
- GetPort(¤tPort); //• save the current grafport (IM I-447).
- SetPort(ascii_window); //• set the grafport to our window.
-
- if (scope)
- BeginUpdate(ascii_window); //• set the visRgn to only the part.
- //• that needs updating (IM I-292).
- //• otherwise, redraw the whole window.
-
- DrawControls(ascii_window); //• draw the controls in our window.
- EraseRect(&blanket_rect); //• blank out the old data (IM I-177).
- y = 10;
-
- if (the_control == OctPtr) //• determine which control was pressed.
- dataAddr = (Ptr) octdata; //• & cast the address of the 2D array.
- else
- if (the_control == DecPtr) //• into our generic data pointer.
- dataAddr = (Ptr) decdata;
- else
- if (the_control == HexPtr)
- dataAddr = (Ptr) hexdata;
-
- TextFont(4); //• Monaco font for evenly spaced columns (IM I-219).
- TextSize(9); //• these are quickdraw functions.
-
- for (line = 0; line < 22; line++)
- {
-
- //• we have 23 lines per 'page' - use quickdraw to output the data.
- //• the starting address for DrawText is (line# * 47) to get to the.
- //• proper position in the 2D array.
-
- MoveTo(12, y);
- DrawText(dataAddr + (line * 47), 0, 46); //• (IM I-172).
- y = y + 10;
- }
-
- if (scope)
- EndUpdate(ascii_window);
-
- SetPort(currentPort); //• restore the current grafport (IM I-447).
-
- } //• end of Update_The_Display.
-
-
- Add_Some_Controls() //• open the desk accessory.
- {
- GrafPtr currentPort;
-
- GetPort(¤tPort); //• save the current grafport.
-
- ascii_window = NewWindow(0, &bounds, "\pAscii Chart", 0, 16, (WindowPtr)-1L, 1, 0);
- // ((WindowPeek) (ascii_window))->windowKind = dce->dCtlRefNum;
-
- // SetPort(ascii_window); //• set the grafport to our window.
- ShowWindow(ascii_window); //• display the window.
-
-
- //• Create new controls for the various radixes and the about box.
- //• Set the octal radix control to be 'on'... (IM I-319).
- OctPtr = NewControl(ascii_window, &OctRect, "\pOct.", 1, 1, 0, 1, 2, 0);
- HexPtr = NewControl(ascii_window, &HexRect, "\pHex", 1, 0, 0, 1, 2, 0);
- DecPtr = NewControl(ascii_window, &DecRect, "\pDec.", 1, 0, 0, 1, 2, 0);
- //• BinPtr = NewControl(ascii_window, &BinRect, "\pBin.", 1, 0, 0, 1, 2, 0);
- AbPtr = NewControl(ascii_window, &AbRect, "\pFast Quit", 1, 1, 0, 0, 0, 0);
-
- the_control = OctPtr; //• set default for current control button.
-
- //• We do not need to explicitly call "Update_The_Display" because
- //• the open routine will cause an update event to be sent to the
- //• Desk Manager.
-
- SetPort(currentPort); //• restore original grafport.
-
- return(0);
-
- }
-
- Do_Content_Click (EventRecord *theEvent)
- {
- //• convert the location to local coordinates (IM I-193, 323).
- GlobalToLocal(&(theEvent->where));
-
- //• find where mouse was pressed - (the part code) (IM I-334).
-
- switch (FindControl(theEvent->where, ascii_window, &whichControl))
- {
- case (11): //• a RADIO button has been hit - track it.
-
- //• track control routine makes sure mouse is released in button.
- if (TrackControl(whichControl, theEvent->where, 0))
- {
- if (whichControl != the_control)
- { //• a new radix.
- SetCtlValue(the_control, 0);
- the_control = whichControl; //• set new 'current' ctl.
- SetCtlValue(the_control, 1);
- Update_The_Display(0); //• update whole window.
- } //• end new radix.
- } //• end if track control.
- break;
-
- case (10): //• SIMPLE button (must be the about box).
- Clean_Up_Things();
- break;
-
- } //• end possible control mousedown.
- LocalToGlobal(&theEvent->where); //• put the coordinates back to global.
- }
-
- //• close DA function. (What DA?)
-
- Clean_Up_Things()
- {
- GrafPtr currentPort;
-
- GetPort(¤tPort); //• save the system's grafport.
- DisposeWindow(ascii_window); //• dispose our window.
- ascii_window = 0L; //• set the driver's window ptr to null.
- //• (IM I-446).
- SetPort(currentPort); //• restore the system's grafport.
- ExitToShell();
-
- }
-
- void Set_Up_The_Window(void)
- {
- dragRect = qd.screenBits.bounds;
-
- ascii_window = NewWindow(0, &bounds, "\pAscii Chart", 0, 16, (WindowPtr)-1L, 1, 0);
- SetPort(ascii_window);
- }
-
- void Set_Up_The_Menus(void)
- {
- InsertMenu(appleMenu = NewMenu(appleID, "\p\024"), 0);
- InsertMenu(fileMenu = NewMenu(fileID, "\pFile"), 0);
- InsertMenu(editMenu = NewMenu(editID, "\pEdit"), 0);
- InsertMenu(wasteMenu = NewMenu(wasteID, "\pUseless Menu"), 0);
- DrawMenuBar();
- AddResMenu(appleMenu, 'DRVR');
- AppendMenu(fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
- AppendMenu(editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
- AppendMenu(wasteMenu, "\p(-;(-;(-;(-;(-;(-;(-;(-;(-;(-;(-;(-;(-;(-;Told Ya");
- }
-
- static void enable(MenuHandle menu, short item, short ok)
- {
- if (ok)
- EnableItem(menu, item);
- else
- DisableItem(menu, item);
- }
-
- void Make_Menu_Adjustments(void)
- {
- register WindowPeek wp = (WindowPeek) FrontWindow();
- short kind = wp ? wp->windowKind : 0;
- Boolean DA = kind < 0;
-
- enable(editMenu, 1, DA);
- enable(editMenu, 3, DA);
- enable(editMenu, 4, DA);
- enable(editMenu, 5, DA);
- enable(editMenu, 6, DA);
-
- enable(fileMenu, openItem, !((WindowPeek) ascii_window)->visible);
- enable(fileMenu, closeItem, DA || ((WindowPeek) ascii_window)->visible);
- }
-
- void Handle_The_Menus (long mSelect)
- {
- int menuID = HiWord(mSelect);
- int menuItem = LoWord(mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
-
- switch (menuID)
- {
- case appleID:
- GetPort(&savePort);
- GetItem(appleMenu, menuItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- break;
-
- case fileID:
- switch (menuItem)
- {
- case openItem:
- ShowWindow(ascii_window);
- SelectWindow(ascii_window);
- break;
-
- case closeItem:
- if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
- break;
-
- if (frontWindow->windowKind < 0)
- CloseDeskAcc(frontWindow->windowKind);
- else if ((frontWindow = (WindowPeek) ascii_window) != NULL)
- HideWindow(ascii_window);
- break;
-
- case quitItem:
- ExitToShell();
- break;
- }
- break;
-
- case editID:
- if (!SystemEdit(menuItem-1))
- SysBeep(5);
- break;
-
- case wasteID:
- break;
- }
- }
-
- void Initialize_The_Managers(void)
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
-
- void Handle_Mouse_Clicks (EventRecord *theEvent)
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- Make_Menu_Adjustments();
- Handle_The_Menus(MenuSelect(theEvent->where));
- break;
-
- case inDrag:
- if (theWindow == ascii_window)
- DragWindow(ascii_window, theEvent->where, &dragRect);
- break;
-
- case inContent:
- Do_Content_Click (theEvent);
- // if (theWindow == ascii_window)
- // {
- // if (theWindow != FrontWindow())
- // SelectWindow(ascii_window);
- // else
- // InvalRect(&ascii_window->portRect);
- // }
- break;
-
- case inGoAway:
- if (theWindow == ascii_window &&
- TrackGoAway(ascii_window, theEvent->where))
- ExitToShell ();
- break;
- }
- }
-
- void Handle_The_Events(void)
- {
- int ok;
- EventRecord theEvent;
-
- HiliteMenu(0);
- SystemTask (); //• Handle desk accessories.
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- Handle_Mouse_Clicks(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- Make_Menu_Adjustments();
- Handle_The_Menus(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- case updateEvt:
- Update_The_Display(1);
- break;
-
- case activateEvt:
- Update_The_Display(1);
- break;
- }
- }
-
- void main( void)
- {
- Initialize_The_Managers();
- Set_Up_The_Menus();
- Set_Up_The_Window();
- Add_Some_Controls();
-
- for (;;)
- Handle_The_Events();
- }
-